/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.leiam.eolica.denis;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

/**
 * 
 * @author denis
 */
public class RosaDosVentosVelocidadesDirecao extends JPanel {
	private int xc = 0, yc = 0;// ponto x e y do centro
	private ResultSetTableModel tableModel;
	private boolean haveData = false;

	public RosaDosVentosVelocidadesDirecao() {

	}

	protected void paintComponent(Graphics g1) {
		super.paintComponent(g1);
		Graphics2D g = (Graphics2D) g1;
		int recuo = 0;
		xc = this.getWidth() / 2;
		yc = this.getHeight() / 2;
		g.setColor(Color.white);
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
		g.setColor(Color.darkGray);

		float[] Dashes = { 10.0F, 5.0F, 5.0F, 5.0F };
		g.setStroke(new BasicStroke(1f, BasicStroke.CAP_SQUARE,
				BasicStroke.JOIN_MITER, 10F, Dashes, 0F));
		g.drawLine(this.getWidth() - 1, 1, this.getWidth() - 1, yc);
		g.setStroke(new BasicStroke(1f));
		// g.drawLine(xc, 1, this.getWidth()-1, 1);
		// parte dos 100%
		g.setColor(Color.white);
		g.fillArc(recuo, recuo, this.getWidth() - 2 * recuo - 1,
				this.getHeight() - 2 * recuo - 1, 0, 360);
		g.setColor(Color.black);
		g.drawArc(recuo, recuo, this.getWidth() - 2 * recuo - 1,
				this.getHeight() - 2 * recuo - 1, 0, 360);
		// parte dos 75%
		int ix75 = (int) (xc - this.getWidth() * .75 / 2);
		int ixc75 = (int) (this.getWidth() * .75);
		int iy75 = (int) (yc - this.getHeight() * .75 / 2);
		int iyc75 = (int) (this.getHeight() * .75);
		g.drawArc(ix75, iy75, ixc75, iyc75, 0, 360);

		// parte dos 50%
		int ix50 = (int) (xc - this.getWidth() * .5 / 2);
		int ixc50 = (int) (this.getWidth() * .5);
		int iy50 = (int) (yc - this.getHeight() * .5 / 2);
		int iyc50 = (int) (this.getHeight() * .5);
		g.drawArc(ix50, iy50, ixc50, iyc50, 0, 360);

		// parte dos 25%
		int ix25 = (int) (xc - this.getWidth() * .25 / 2);
		int ixc25 = (int) (this.getWidth() * .25);
		int iy25 = (int) (yc - this.getHeight() * .25 / 2);
		int iyc25 = (int) (this.getHeight() * .25);
		g.drawArc(ix25, iy25, ixc25, iyc25, 0, 360);

		// angulos
		// 0
		g.drawLine(recuo, yc, this.getWidth() - recuo, yc);
		// 90
		g.drawLine(xc, recuo, xc, this.getHeight() - recuo);

		if (haveData) {

			g.setColor(Color.blue);
			int i = 0, x = 75;
			double vlrMax = 0;
			for (i = 0; i < tableModel.getRowCount(); i++) {
				String valors = "0";
				double temp = 0;
				try {
					valors = tableModel.getValueAt(i, 1).toString();
				} catch (Exception e) {
					valors = "0";
				}

				temp = Double.valueOf(valors);
				if (temp >= vlrMax) {
					while (temp > vlrMax)
						vlrMax = vlrMax + 2;
				}
			}
			for (i = 0; i < tableModel.getRowCount(); i++) {
				double valorx = 0;
				double valory = 0;
				String valors = "0";
				double temp = 0;
				try {
					valors = tableModel.getValueAt(i, 1).toString();
				} catch (Exception e) {
					valors = "0";
				}

				// g.setColor(Color.getHSBColor((float)x-15, x-15, x-15));
				g.setColor(Color.blue);
				temp = Double.valueOf(valors);
				// valorx=(temp*100/20)*(this.getWidth()/100);
				// valory=(temp*100/20)*(this.getHeight()/100);
				valorx = (temp / vlrMax) * (this.getWidth());
				valory = (temp / vlrMax) * (this.getHeight());

				// System.out.println(valory);
				g.fillArc(xc - (int) valorx / 2, yc - (int) valory / 2,
						(int) valorx, (int) valory, x + 2, -28);
				// System.out.println(x);
				x = x - 30;

			}
			g.setColor(Color.darkGray);
			g.setFont(g.getFont().deriveFont(
					(float) (this.getWidth()) * 14f / 300f));
			g.drawString(String.valueOf(vlrMax) + " m/s", this.getWidth() - 50
					* this.getWidth() / 300, 12);
			tableModel.disconnectFromDatabase();
		}
	}

	/**
	 * @return the tableModel
	 */
	public ResultSetTableModel getTableModel() {
		return tableModel;
	}

	/**
	 * @param tableModel
	 *            the tableModel to set
	 */
	public void setTableModel(ResultSetTableModel tableModel) {
		this.tableModel = tableModel;
		haveData = true;
		// this.repaint();
	}
}
